@isTest
private class LoanControllerTest 
{
    
    private static void createTestData()
    {
        LenderLoan__c lenderLoan;
        RecordType recordType = [Select id From RecordType Where Name = 'Lender'];
        Account account = new Account(recordtypeid=recordType.id);
        account.name = 'Giles';
        Database.insert(account);
        lenderLoan = new LenderLoan__c();
        lenderLoan.lender__c = account.id;
        lenderLoan.commissionPercentage__c = 35;
        Database.insert(lenderLoan);
        account = new Account();
        account.name = 'Pete';
        Database.insert(account);
        account = new Account(recordtypeid=recordType.id);
        account.name = 'Giles';
        Database.insert(account);
        lenderLoan = new LenderLoan__c();
        lenderLoan.lender__c = account.id;
        lenderLoan.commissionPercentage__c = 35;
        Database.insert(lenderLoan);
        account = new Account(recordtypeid=recordType.id);
        account.name = 'John';
        Database.insert(account);
        lenderLoan = new LenderLoan__c();
        lenderLoan.lender__c = account.id;
        lenderLoan.commissionPercentage__c = 35;
        Database.insert(lenderLoan);
    }
    
    static testMethod void testSetRefferCode()
    {
        PageReference pageRef = Page.home;
        pageRef.getParameters().put(LoanController.REFERRER_CODE, 'test');
        Test.setCurrentPage(pageRef);
        LoanController controller = new LoanController();
        System.assert(controller.loanRequest.referrerCode == null);     
    }
    
    
    static testMethod void testCalculateRefunds()
    {
        createTestData();
        //Gotta create a commission breakdown
        //Gotta create a bunch of lenders and lender loans
        commissionBreakdown__c commissionBreakdown  = new commissionBreakdown__c();
        commissionBreakdown.Name = 'Franchisee';
        commissionBreakdown.percentageToCustomer__c = 50;
        commissionBreakdown.percentageToFranchisee__c = 30;
        commissionBreakdown.percentageToRd__c = 20;
        insert commissionBreakdown;
        commissionBreakdown  = new commissionBreakdown__c();
        commissionBreakdown.Name = 'Standard';
        commissionBreakdown.percentageToCustomer__c = 50;
        commissionBreakdown.percentageToRd__c = 50;
        insert commissionBreakdown;
        RecordType recordType = [Select id, Name From RecordType Where Name = 'Franchisee'];
        System.debug('Record type --> ' + recordType.name);
        Account account = new Account(recordtypeid=recordType.id);
        account.name = 'Giles';
        insert(account);
        /**
        * IMPORTANT - Salesforce will not create an auto number on test cases unless specifically
        * enabled by salesforce which makes it slightly difficult to test referrer code functionality
        */
        System.debug('Account id --> ' + account.id);
        System.debug('Account referrer code --> ' + account.referrerCode__c);
        
        createTestData();
        PageReference pageRef = Page.home;
        pageRef.getParameters().put(LoanController.REFERRER_CODE, account.referrerCode__c);
        Test.setCurrentPage(pageRef);
        LoanController controller = new LoanController();
        controller.loanRequest.loanType = 'Home or Investment Loan';
        controller.loanRequest.amount= '100000';
        controller.loanRequest.timeFrame= '2 days';
        controller.loanRequest.duration = 'blank';
        
        List<lenderLoan__c> loans = [Select id, name, lender__r.name from lenderLoan__c where 
                loanType__c = 'Home Loan' order by lender__r.name];
        
        controller.eventCalculateRefunds();
        System.debug('LOANS SIZE: ' + controller.loanRequest.loans.size());
        System.assert(controller.loanRequest.loans.size() > 0);
        controller.eventValidateChosenLenders();
        
    }
    
    static testMethod void testSubmitLoanRequest()
    {
        createTestData();
        LoanController loanController = new LoanController();
        loanController.loanRequest.salutation = 'Mr';
        loanController.loanRequest.firstName = 'Giles';
        loanController.loanRequest.lastName = 'Bill';
        loanController.loanRequest.email = 'giles.bill@test.com';
        loanController.loanRequest.amount  = '100000';
        loanController.loanRequest.permissionToSubmit = true;
        loanController.loanRequest.amount = '100';
        loanController.loanRequest.loanType = 'property';
        loanController.loanRequest.timeFrame = '1 day';
        loanController.loanRequest.currentBank = 'Test';
        loanController.loanRequest.phone = '0288889999';
        RecordType recordType = [Select id From RecordType Where Name = 'Lender'];
        List<LenderLoan__c> lenderLoans = [select id from LenderLoan__c limit 3];
        Loan loan;
        Account account;
        for(LenderLoan__c lenderLoan : lenderLoans)
        {
            account = new Account();
            account.name = 'Giles';
            Database.insert(account);
            loan = new Loan();  
            loan.request = new loanRequest__c();
            loan.request.lenderLoan__c = lenderLoan.id;
            loan.selected = true;
            loan.request.name = 'whatever';
            lenderLoan.lender__c = account.id;
            loan.lender = lenderLoan;
            loanController.loanRequest.loans.add(loan);
        }
        loanController.eventSubmitLoanRequest();
        System.assert(loanController.isRegistered('giles.bill@test.com'));
        recordType = [Select id From RecordType Where Name = 'Customer'];
        account = [Select id from Account where recordTypeId = :recordType.id and PersonEmail = 'giles.bill@test.com' limit 1];
        System.assert(account != null);
        Opportunity opportunity = [Select id from Opportunity where AccountID = :account.id limit 1];
        System.assert(opportunity != null);
        List<loanRequest__c> loanRequests = [Select id, name from loanRequest__c where opportunity__c = :opportunity.id];
        System.debug('Loan requests --> ' + loanRequests.size());
        System.assert(loanRequests.size() > 0);
        loanController.getLoanTypes();
        loanController.getLoanDurations();
        loanController.getPreferredContactTimes();
    }
    
    static testMethod void testGetLoanTypes()
    {
        LoanController loanController = new LoanController();
        List<SelectOption>options = loanController.getLoanTypes();
        System.assert(options.size() > 0);
    }
    
    static testMethod void testRest()
    {
        LoanController controller = new LoanController();
        LoanController.getFranchise('test');
        controller.loanRequest.salutation = 'Mr';
        controller.loanRequest.firstName = 'Giles';
        controller.loanRequest.lastName = 'Bill';
        controller.loanRequest.email = 'giles.bill@test.com';
        controller.loanRequest.amount  = '100000';
        controller.loanRequest.permissionToSubmit = true;
        controller.loanRequest.amount = '100';
        controller.loanRequest.loanType = 'property';
        controller.loanRequest.timeFrame = '1 day';
        controller.loanRequest.currentBank = 'Test';
        controller.loanRequest.phone = '0288889999';
        controller.eventApplicationStepOne();
        controller.eventApplicationStepOneBack();
        controller.eventApplicationStepTwoBack();
        controller.eventApplicationStepThreeBack();
    }
    
    
}